package com.ruoyi.web.controller.mongo;

import com.alibaba.fastjson2.JSONObject;
import com.mongodb.client.result.UpdateResult;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mongo.service.MongoConstants;
import com.ruoyi.mongo.word.entity.WordChinToEngEntity;
import com.ruoyi.mongo.word.entity.WordHearRecord;
import com.ruoyi.mongo.word.entity.WordStudyEntity;
import com.ruoyi.mongo.word.entity.WordTestEntity;
import com.ruoyi.mongo.word.service.WordCheckRecordInterface;
import com.ruoyi.mongo.word.service.WordListInterface;
import com.ruoyi.mongo.word.service.WordListMongoInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController()
@RequestMapping("/word")
public class WordController {

    @Autowired
    private WordListMongoInterface wordListMongoInterface;

    @Autowired
    private WordCheckRecordInterface wordCheckRecordInterface;

    @Autowired
    private WordListInterface wordListInterface;

    /**
     * 全部数据
     */
    @GetMapping("/list")
    public AjaxResult getList() {
        List<?> word = wordListMongoInterface.getAllListData(MongoConstants.COLLECT_WORD);

        return AjaxResult.success(word);
    }

    /**
     * 全部数据
     * 分页查询
     */
    @GetMapping("/listPage")
    public AjaxResult getListPage(@RequestParam int pageNum, @RequestParam int pageSize) {
        List<?> dataByPage = wordListMongoInterface.getAllListDataByPage(MongoConstants.COLLECT_WORD, pageNum, pageSize);
        return AjaxResult.success(dataByPage);
    }

    /**
     * 单元、书名查询
     */
    @GetMapping("/listUnit")
    public AjaxResult getListUnit(@RequestParam String unit, @RequestParam String book) {
        List<?> dataByUnit = wordListMongoInterface.getAllListDataByUnit(MongoConstants.COLLECT_WORD, unit, book);
        return AjaxResult.success(dataByUnit);
    }

    /**
     * 按单元查询
     * 有测试记录且小于单元数5，生成一半的单元测试单词
     */
    @GetMapping("/listUnitCheck")
    public AjaxResult getListUnitCheck(@RequestParam(required = false) String unit) {
        List<?> dataByUnit = wordListMongoInterface.getAllListDataByUnitCheck(MongoConstants.COLLECT_WORD, unit);
        if (dataByUnit == null || dataByUnit.isEmpty()) {
            return AjaxResult.error("未查询到数据");
        }
        return AjaxResult.success(dataByUnit);
    }

    /**
     * 按单元查询
     * 按测试范围生成数据
     */
    @GetMapping("/listUnitTimes")
    public AjaxResult getListUnitTimes(@RequestParam(required = false) String unit,
                                       @RequestParam(required = false) int times,
                                       @RequestParam(required = false) Integer pageNum,
                                       @RequestParam(required = false) String book) {
        List<?> dataByUnit = wordListMongoInterface.getAllListDataByUnitCheck(MongoConstants.COLLECT_WORD, unit, book, times, pageNum);
        return AjaxResult.success(dataByUnit);
    }

    @PostMapping("/addTest")
    public AjaxResult addTestRecord(@RequestBody List<WordTestEntity> wordTestEntityList) {
        wordListMongoInterface.insertTestList(wordTestEntityList, MongoConstants.COLLECT_WORD_TEST);
        return AjaxResult.success();
    }

    @PostMapping("/insertList")
    public AjaxResult insertCheckRecordList(@RequestBody JSONObject jsonObject) {
        int insertedSize = wordCheckRecordInterface.insertRecordList(jsonObject, MongoConstants.COLLECT_WORD_CHECK_NEW_RECORD);
        return AjaxResult.success(insertedSize);
    }

    @DeleteMapping("/removeTest/{id}")
    public AjaxResult removeTestRecord(@PathVariable String id) {
        if (id.isEmpty()) {
            return AjaxResult.warn("主键不能为空");
        }
        wordListMongoInterface.removeTestData(MongoConstants.COLLECT_WORD_TEST, id);
        return AjaxResult.success();
    }

    @PostMapping("/putTestList")
    public AjaxResult putTestList(@RequestBody List<WordTestEntity> wordTestEntityList) {
        if (wordTestEntityList.isEmpty()) {
            return AjaxResult.warn("测试记录为空");
        }
        ArrayList<UpdateResult> results = wordListMongoInterface.insertOrUpdateTestList(wordTestEntityList, MongoConstants.COLLECT_WORD_TEST);
        return AjaxResult.success(results);
    }

    @PostMapping("/putTestData")
    public AjaxResult putTestData(@RequestBody WordTestEntity wordTest) {
        ArrayList<UpdateResult> results = wordListMongoInterface.insertOrUpdateTestList(wordTest, MongoConstants.COLLECT_WORD_TEST);
        return AjaxResult.success(results);
    }

    @GetMapping("/listTestPage")
    public AjaxResult getListTestPage(@RequestParam(required = false) String type,
                                      @RequestParam String book,
                                      @RequestParam(required = false) Integer pageNum) {
        List<?> dataByPage = wordListMongoInterface.getListTestData(MongoConstants.COLLECT_WORD_TEST, type, book, pageNum);
        return AjaxResult.success(dataByPage);
    }

    /**
     * 测试详情
     * 列表
     */
    @GetMapping("/getTestDetailsList")
    public AjaxResult getTestDetailsList(@RequestParam(required = false) String status,
                                         @RequestParam(required = false) int way,
                                         @RequestParam String practice) {
        List<?> data = wordListMongoInterface.getTestDetailsList(status, practice,way);
        return AjaxResult.success(data);
    }

    /**
     * 汉译英详情
     * 列表
     */
    @GetMapping("/getWriteDetailsList")
    public AjaxResult getWriteDetailsList(@RequestParam(required = false) String status,
                                         @RequestParam String practice) {
        List<?> data = wordListMongoInterface.getWriteDetailsList(status, practice);
        return AjaxResult.success(data);
    }

    /**
     * 汉译英详情
     * 列表
     */
    @GetMapping("/writeDetailsList")
    public AjaxResult getWriteDetailsList(@RequestParam(required = false) int userId,
                                          @RequestParam String practice) {
        List<?> data = wordListMongoInterface.getWriteDetailsList(userId, practice);
        return AjaxResult.success(data);
    }

    /**
     * 学习详情
     * 用户
     * 列表
     */
    @GetMapping("/getStudyDetailsListByUser")
    public AjaxResult getStudyDetailsListByUser(@RequestParam(required = false) int userId,
                                                @RequestParam(required = false) int way,
                                               @RequestParam String practice) {
        List<?> data = wordListMongoInterface.getStudyDetailsListByUser(userId, practice,way);
        return AjaxResult.success(data);
    }

    /**
     * 测试详情
     * 用户
     * 列表
     */
    @GetMapping("/getTestDetailsListByUser")
    public AjaxResult getTestDetailsListByUser(@RequestParam int userId,
                                               @RequestParam(required = false) int way,
                                               @RequestParam(required = false) String type,
                                               @RequestParam String practice) {
        List<?> data = wordListMongoInterface.getTestDetailsListByUser(userId,way,type, practice);
        return AjaxResult.success(data);
    }

    /**
     * 听力详情
     * 列表
     */
    @GetMapping("/hearDetailList")
    public AjaxResult getHearDetailsList(@RequestParam(required = false) String status,
                                         @RequestParam String practice) {
        List<?> data = wordListMongoInterface.getHearDetailsList(status, practice);
        return AjaxResult.success(data);
    }

    /**
     * 听力详情
     * 用户
     * 列表
     */
    @GetMapping("/hearDetailListByUser")
    public AjaxResult getHearDetailsListByUser(@RequestParam int userId,
                                               @RequestParam String practice) {
        List<?> data = wordListMongoInterface.getHearDetailsListByUser(userId, practice);
        return AjaxResult.success(data);
    }

    /**
     * 学习记录
     * 列表
     * 保存
     */
    @PostMapping("/putStudyList")
    public AjaxResult putStudyData(@RequestBody List<WordStudyEntity> list) {
        if (list.isEmpty()) {
            return AjaxResult.warn("学习记录为空");
        }
        wordListMongoInterface.insertStudyList(list, MongoConstants.COLLECT_WORD_STUDY);
        return AjaxResult.success();
    }

    /**
     * 学习记录
     * 单个
     * 保存
     */
    @PostMapping("/putStudyData")
    public AjaxResult putStudyData(@RequestBody WordStudyEntity entity) {
        WordStudyEntity studyEntity = wordListMongoInterface.insertStudyData(entity, MongoConstants.COLLECT_WORD_STUDY);
        return AjaxResult.success(studyEntity);
    }

    /**
     * 汉译英记录-保存
     * 单个
     */
    @PostMapping("/chinToEngData")
    public AjaxResult putChinToEngData(@RequestBody WordChinToEngEntity entity) {
        wordListMongoInterface.insertStudyData(entity, MongoConstants.COLLECT_WORD_CHIN_TO_ENG);
        return AjaxResult.success(DateUtils.getLocalTime());
    }

    /**
     * 测试方法
     * */
    @PostMapping("/insertOneDataCommon")
    public AjaxResult insertOneDataCommon(@RequestBody WordChinToEngEntity entity){
        wordListInterface.insertStudyData(entity,MongoConstants.COLLECT_WORD_CHIN_TO_ENG);
        return AjaxResult.success(DateUtils.getLocalTime());
    }

    /**
     * 学习记录
     * 列表
     */
    @GetMapping("/listStudyPage")
    public AjaxResult getListStudyPage(@RequestParam(required = false) String unit,
                                       @RequestParam(required = false) String book,
                                       @RequestParam(required = false) Integer pageNum) {
        List<?> dataByPage = wordListMongoInterface.getListStudyData(MongoConstants.COLLECT_WORD_STUDY, unit, book, pageNum);
        return AjaxResult.success(dataByPage);
    }


    /**
     * 听力练习
     * 默认条数-30
     * 列表
     */
    @GetMapping("/hear/getList")
    public AjaxResult getHearList(@RequestParam(required = false) Integer times) {
        List<?> hearList = wordListMongoInterface.getHearList(times);
        return AjaxResult.success(hearList);
    }

    /**
     * 听力练习
     * 通过分组(1-50)
     * 列表
     */
    @GetMapping("/hear/getListByGroup")
    public AjaxResult getHearListByGroup(@RequestParam String group) {
        List<?> hearList = wordListMongoInterface.getHearListByGroup(group);
        return AjaxResult.success(hearList);
    }

    /**
     * 听力练习
     * 查询
     * 单个记录
     * 需要修改
     */
    @GetMapping("/hear/getOne/{id}")
    public AjaxResult getHearOne(@PathVariable String id) {
        WordHearRecord hearOne = wordListMongoInterface.getHearOne(id);
        return AjaxResult.success(hearOne);
    }

    /**
     * 听力练习
     * 保存
     * 单个记录
     */
    @PostMapping("/hear/putData")
    public AjaxResult getHearList(@RequestBody WordHearRecord entity) {
        wordListMongoInterface.putHearData(entity);
        return AjaxResult.success("保存成功");
    }

    /**
     * 听力练习
     * 修改
     * 单个记录
     */
    @PostMapping("/hear/updateData")
    public AjaxResult updateHearList(@RequestBody WordHearRecord entity) {
        wordListMongoInterface.updateHearData(entity);
        return AjaxResult.success("修改成功");
    }
}
